package com.cfh.web.xml;

import java.awt.Color;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.patchca.color.ColorFactory;
import org.patchca.color.SingleColorFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;

import com.cfh.ahead.Constant;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class ImageAction extends ActionSupport implements SessionAware {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ImageAction.class);

	private Map session;

	public void setSession(Map map) {
		session = map;
	}

	private static ConfigurableCaptchaService cs = null;
	private static ColorFactory cf = null;
	private static RandomWordFactory wf = null;
	private static WobbleRippleFilterFactory wrff = null;

	static {
		cs = new ConfigurableCaptchaService();
		cf = new SingleColorFactory(new Color(25, 60, 170));
		wf = new RandomWordFactory();
		// wf.setCharacters("2345678acefghkmnruwxyz");
		wf.setCharacters("123456789");
		wrff = new WobbleRippleFilterFactory();
		cs.setWordFactory(wf);
		cs.setColorFactory(cf);

		try {
			cs.setWidth(Integer.valueOf(Tools.getProperties("captcha.width")));
			cs.setHeight(Integer.valueOf(Tools.getProperties("captcha.height")));
			wf.setMaxLength(Integer.valueOf(Tools.getProperties("captcha.maxlength")));
			wf.setMinLength(Integer.valueOf(Tools.getProperties("captcha.minlength")));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("imageAction static error:", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * Logger for this class
	 */

	private static final long serialVersionUID = 1L;

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		try {
			session.put(Constant.CAPTCHA_SESSION_HOLDER, Constant.CAPTCHA_SESSION_INIT_VALUE);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("image/png");
			response.setHeader("cache", "no-cache");
			OutputStream os = response.getOutputStream();
			cs.setFilterFactory(wrff);
			String captcha = EncoderHelper.getChallangeAndWriteImage(cs, "png", os);
			if (logger.isDebugEnabled()) {
				logger.debug("execute()"); //$NON-NLS-1$
			}
			session.put(Constant.CAPTCHA_SESSION_HOLDER, captcha.toLowerCase());
			os.flush();
			os.close();
		} catch (javax.imageio.IIOException iioe) {
			if (logger.isDebugEnabled()) {
				logger.debug("IIOException"); //$NON-NLS-1$
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("imageAction error:", e);
			throw new RuntimeException(e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return null;
	}

}
